/*
 * @Description: 
 * @Author: xuechengwu
 * @Email: xuechengwu@myhexin.com
 * @Date: 2021-11-20 15:18:17
 * @LastEditTime: 2022-02-09 15:25:04
 * @LastEditors: xuechengwu
 */
import Vue from 'vue';
export function createPage() {
    return {
        mark: Math.random().toFixed(16).substring(2),
        name: '未命名',
        components: [],
        interfaces: [],
        values: {}, //  接口请求中的数据
        datas: {
            test: {
                name: '测试数据',
                value: 'test',
                key: 'test',
            }
        }, // 页面中的数据
        style: {},
        currentComponent: '',
        currentComponentParent: [],
        propperties: [],
    }
}
export function loadJS(url){
    return new Promise((resolve) => {
        const script = document.createElement('script');
        script.type = 'text/javascript';
        //IE
        if(script.readyState){
            script.onreadystatechange = function(){
                if( script.readyState == 'loaded' || script.readyState == 'complete' ){
                    script.onreadystatechange = null;
                    resolve();
                }
            };
        }else{
            //其他浏览器
            script.onload = function(){
                resolve();
            };
        }
        script.src = url;
        document.getElementsByTagName('head')[0].appendChild(script);
    });
    
}
/**
 * 根据路径钻取某个对象上的数据
 */
export function obtain(path, obj, defaultValue = '') {
    return path.split('.').reduce((obj, key, currentIndex, arr) => {
        if (obj[key] !== undefined) {
            return obj[key];
        }
        return !defaultValue ? arr.length - 1 === currentIndex ? '' : {} : defaultValue;
    }, obj);
}

export function updateObject(path, value, obj, lastObj) {
    path.split('.').reduce((cur, key, index, array) => {
        if (index === array.length - 1) {
        if (/^\d+$/.test(key)) {
            const target = [...cur];
            target[key] = value;
            lastObj[array[index - 1]] = target;
            return lastObj;
        }
        if (cur[key] === undefined) {
            Vue.set(cur, key, value);
        } else {
            cur[key] = value;
        }
        } else if (cur[key] === undefined){
        Vue.set(cur, key, {});
        }
        lastObj = cur;
        return cur[key];
    }, obj);
}